package app.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import app.ApplicationManager;

/**
 * for loading application groups and applications in a profile
 * 
 * @author eduard
 * 
 */
public class ProfileLoad {
	public String name;
	// Security level to application groups
	private List<ResSecLevel> groups;
	// Security level to applications
	private List<ResSecLevel> apps;

	public ProfileLoad() {
		groups = new ArrayList<ResSecLevel>();
		apps = new ArrayList<ResSecLevel>();
	}

	/**
	 * Gets application name and application secutity level
	 * 
	 * @param apm
	 * @return
	 */
	public Profile getProfile(ApplicationManager apm) {
		
		AppGroup appGroup;		
		Integer secLevel;
		
		Profile profile = new Profile(name);
		HashMap<String, Integer> appsProfile = profile.getApps();
		
		for (ResSecLevel rsl : getApps()) 
			appsProfile.put(rsl.getName(), rsl.getSecLevel());
		
		
		for (ResSecLevel rsl : getGroups()) {
			
			appGroup = apm.getGroup(rsl.getName());
			
			for (String appNames : appGroup.getAppNames()) {
				
				secLevel = appsProfile.get(appNames);
				if (secLevel == null || secLevel < rsl.getSecLevel()) {
					appsProfile.put(appNames, rsl.getSecLevel());
				}
			}

		}
		return profile;
	}

	public List<ResSecLevel> getGroups() {
		return groups;
	}

	public void setGroups(List<ResSecLevel> groups) {
		this.groups = groups;
	}

	public List<ResSecLevel> getApps() {
		return apps;
	}

	public void setApps(List<ResSecLevel> apps) {
		this.apps = apps;
	}
}
